<!--
 * @Description: 
 * @Author: 胡涛
 * @Date: 2023-11-19 20:13:41
 * @LastEditors: 胡涛
 * @LastEditTime: 2023-11-19 21:01:47
-->
<template>
  <a-modal
    :open="visible"
    :keyboard="false"
    :maskClosable="false"
    :title="title"
    @cancel="onCancel"
    @ok="onSubmit"
    :confirmLoading="submitLoading"
    :width="700"
  >
    <a-form ref="formRef" v-bind="formLayout" :model="model" :rules="rules">
      <a-form-item label="所属类别" prop="type">
        <enum-select
          v-model:value="model.type"
          name="ReportCategoryTypeEnum"
          disabled
        ></enum-select>
      </a-form-item>
      <a-form-item label="上级分类" prop="parentId">
        <a-tree-select
          v-model:value="model.parentId"
          :treeData="categoryOptions"
          :fieldNames="{ label: 'name', value: 'id', children: 'children' }"
        />
      </a-form-item>
      <a-form-item label="分类名称" prop="name">
        <a-input v-model:value="model.name"></a-input>
      </a-form-item>
      <a-form-item label="启用状态" prop="status">
        <a-switch v-model:checked="model.status"> </a-switch>
      </a-form-item>
      <a-form-item label="分类描述" prop="remarks">
        <a-input v-model:value="model.remarks"></a-input>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script lang="ts" setup>
import { ReportCategoryConfigDto } from "@/api/report/types";
import { useFormPage } from "@/helpers/pages";
import { Empty, FormInstance } from "ant-design-vue";
import { computed, onMounted, ref, watch } from "vue";
import api from "@/api/report/reportCategoryConfig";
import { EmptyGuid } from "@/helpers";
const formRef = ref<FormInstance>();
const emits = defineEmits(["update:visible", "close", "success"]);
const props = defineProps({
  id: String,
  visible: { type: Boolean, default: false },
  type: { type: Number, default: 1 },
  action: {
    type: String,
    default: "create",
  },
  categoryItems: { type: Array, required: true },
  item: {
    type: Object,
    default() {
      return {};
    },
  },
});

const { model, rules, formLayout, loading, submitLoading } = useFormPage<
  ReportCategoryConfigDto,
  string
>({
  rules: {
    type: [{ required: true, message: "请选择分类大类别" }],
    parentId: [{ required: true, message: "请选择分类大类别" }],
    name: [{ required: true, message: "请选择分类大类别" }],
  },

  model: {
    type: 1,
    name: "",
    remarks: "",
    status: true,
  },
});

const isEdit = computed(() => {
  return props.action === "edit";
});

const categoryOptions = computed(() => {
  let items: Array<any> = [
    { name: "用作一级分类", id: EmptyGuid },
    ...props.categoryItems,
  ];
  return items;
});

/**
 * 弹窗标题
 */
const title = computed(() => {
  return isEdit.value ? "修改分类信息" : "添加分类信息";
});

/**
 * 加载数据
 */
const loadData = () => {
  model.type = props.type;
  if (isEdit.value) {
    loading.value = true;
    api.get(props.id).then((res) => {
      Object.assign(model, res);
      loading.value = false;
    });
  }
};

/**
 * 提交
 */
const onSubmit = () => {
  emits("success");
  formRef.value.validate().then(() => {
    let request = isEdit.value
      ? api.update(props.id, model)
      : api.create(model);
    submitLoading.value = true;
    request.then((res) => { 
      emits("success", res);
      emits("update:visible", false);
      emits("close");
    })
      .finally(() => {
        submitLoading.value = false;
      });
  });
};

/**
 * 取消
 */
const onCancel = () => {
  emits("update:visible", false);
  emits("close");
};

watch(
  () => props.visible,
  (val: boolean) => {
    console.log("visible changed,val,", val);
    val && loadData();
  },
  { immediate: true }
);
</script>

<style lang="less" scoped>
.row {
  .cell {
    padding: 5px 8px;
  }

  &-header {
    background: rgba(0, 0, 0, 0.05);
    border-bottom: 1px solid #eee;
    .cell {
      font-weight: bold;
    }
  }

  &-footer {
    padding: 5px 8px;
  }
}
</style>
